package com.pengweiwei.androidzygote.video;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;

import com.google.android.material.tabs.TabLayout;
import com.pengweiwei.androidzygote.R;
import com.pengweiwei.androidzygote.databinding.FragmentHomeQualityBinding;
import com.pengweiwei.androidzygote.video.data.VideoApiHelper;
import com.pengweiwei.androidzygote.video.data.beans.FooterBean;
import com.putong35.common.account.AccountManager;
import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.fragment.BaseFragment;
import com.putong35.common.httprequest.OnFailureListener;
import com.putong35.common.httprequest.OnSuccessListener;
import com.putong35.common.utils.EasyToast;

import java.util.List;

@SuppressLint("ValidFragment")
public class HomeQualityFragment extends BaseFragment {

    public HomeQualityFragment() {

    }


    private String currChannel = "";
    private String sort = ""; // 默认按点赞排序

    FragmentHomeQualityBinding binding;


    List<FooterBean> cateList = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setState(CompState.EMPTY_REFRESHING);
        VideoApiHelper.footer(2, new OnSuccessListener<List<FooterBean>>() {
            @Override
            public void onSuccess(int code, String msg, List<FooterBean> data, Object extObj) {
                if (data != null && data.size() > 0) {
                    cateList = data;
                    TabLayout tabMain = binding.tabMain;
                    for (int i = 0; i < data.size(); i++) {
                        FooterBean bean = data.get(i);
                        tabMain.addTab(tabMain.newTab().setText(bean.getName()));
                    }
                    tabMain.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                        @Override
                        public void onTabSelected(TabLayout.Tab tab) {
                            int posi = tabMain.getSelectedTabPosition();
                            currChannel = cateList.get(posi).getId();
                            onChannelChanged(posi, currChannel, cateList.get(posi).getSortVoList());
                        }

                        @Override
                        public void onTabUnselected(TabLayout.Tab tab) {

                        }

                        @Override
                        public void onTabReselected(TabLayout.Tab tab) {

                        }
                    });
//                    tabMain.getTabAt(0).select();
                    currChannel = data.get(0).getId();
                    onChannelChanged(0, currChannel, cateList.get(0).getSortVoList());
                }

                setState(CompState.DATA);
            }
        }, new OnFailureListener() {
            @Override
            public void onFailure(int errCode, String msg) {
                EasyToast.showToast(getActivity(), msg);
//                setState(CompState.EMPTY_ERROR);
            }
        });



        return view;
    }

    /**
     *  @param posi  位置
     * @param channel
     * @param sortVoList
     */
    private void onChannelChanged(int posi, String channel, List<FooterBean.SortVoListBean> sortVoList) {
        // 添加分类UI
        binding.lytCate.removeAllViews();
        if (sortVoList != null && sortVoList.size() > 0) {
            for (int i = 0; i < sortVoList.size(); i++) {
                TabLayout subTabLayout = (TabLayout) getLayoutInflater().inflate(R.layout.tab_layout_quality, null);
                binding.lytCate.addView(subTabLayout);
                List<FooterBean.SortVoListBean.SortValueListBean> sortValueList = sortVoList.get(i).getSortValueList();
                if (sortValueList != null && sortValueList.size() > 0) {
                    TabLayout.Tab tab0 = subTabLayout.newTab().setCustomView(R.layout.tab_item_quality);
                    TextView textView0 = tab0.getCustomView().findViewById(R.id.tvText);
                    textView0.setText("全部");
                    subTabLayout.addTab(tab0);

                    for (int j = 0; j < sortValueList.size(); j++) {
                        FooterBean.SortVoListBean.SortValueListBean sortValueListBean = sortValueList.get(j);
                        // create item view
                        TabLayout.Tab tab = subTabLayout.newTab().setCustomView(R.layout.tab_item_quality);
                        TextView textView = tab.getCustomView().findViewById(R.id.tvText);
                        textView.setText(sortValueListBean.getName());
                        subTabLayout.addTab(tab);
                    }
                }

                subTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        sort = getSelectedSorts();
                        dataFragment.onSelectChanged(sort, currChannel);
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });

            }
        }

        sort = "";
        dataFragment.onSelectChanged(sort, currChannel);
    }

    private String getSelectedSorts() {
        int childCount = binding.lytCate.getChildCount();
        String sort = "";
        for (int i = 0; i < childCount; i++) {
            TabLayout subTabLayout = ((TabLayout) binding.lytCate.getChildAt(i));
            int selectedPosi = subTabLayout.getSelectedTabPosition();
            if (selectedPosi == 0) {
                continue;
            }
            FooterBean.SortVoListBean.SortValueListBean sortValueListBean = cateList.get(binding.tabMain.getSelectedTabPosition()).getSortVoList().get(i).getSortValueList().get(selectedPosi-1);
            sort = sort + sortValueListBean.getId() + ",";
        }

        return sort;
    }

    VideoGridFragment1 dataFragment = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentHomeQualityBinding.inflate(inflater);
        binding.setAccount(AccountManager.getInstance().getAccount());
        FragmentManager fragmentManager = getChildFragmentManager();
        dataFragment  = (VideoGridFragment1) fragmentManager.findFragmentById(R.id.fragData);

        return binding.getRoot();
    }


}
